<template>
  <el-dialog :visible="visible" :title="formTitle" width="40%" :before-close="close">
    <el-form :model="localApiConfig" :rules="rules" ref="form">
      <el-form-item label="名称" prop="name">
        <el-input v-model="localApiConfig.name"></el-input>
      </el-form-item>
      <el-form-item label="基础 URL" prop="baseUrl">
        <el-input v-model="localApiConfig.baseUrl"></el-input>
      </el-form-item>
      <el-form-item label="路径" prop="path">
        <el-input v-model="localApiConfig.path"></el-input>
      </el-form-item>
      <el-form-item label="方法" prop="method">
        <el-select v-model="localApiConfig.method" placeholder="请选择">
          <el-option label="GET" value="GET"></el-option>
          <el-option label="POST" value="POST"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="请求头" prop="requestHeaders">
        <el-input v-model="localApiConfig.requestHeaders" type="textarea" rows="3"></el-input>
      </el-form-item>
      <el-form-item label="请求体" prop="requestBody">
        <el-input v-model="localApiConfig.requestBody" type="textarea" rows="3"></el-input>
      </el-form-item>
      <el-form-item label="请求参数" prop="requestParams">
        <el-input v-model="localApiConfig.requestParams" type="textarea" rows="3"></el-input>
      </el-form-item>
      <el-form-item label="关联权限认证配置" prop="authConfigId">
        <el-select v-model="localApiConfig.authConfig.id" clearable placeholder="请选择 AUTH" style="width: 300px;">
          <el-option
              v-for="item in selectAuthList"
              :key="item.value"
              :label="item.label"
              :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="是否启用" prop="enabled">
        <el-switch v-model="localApiConfig.enabled"></el-switch>
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="close">取消</el-button>
      <el-button type="primary" @click="save">保存</el-button>
    </template>
  </el-dialog>
</template>

<script>
import { getAuthConfigs } from "@/services/apiService";

export default {
  props: {
    apiConfig: {
      type: Object,
      default: () => ({})
    },
    visible: {
      type: Boolean
    }
  },
  data() {
    return {
      selectAuthList: [],
      localApiConfig: {
        ...this.apiConfig,
        authConfig: this.apiConfig.authConfig || {}
      },
      rules: {
        name: [{ required: true, message: '名称不能为空', trigger: 'blur' }],
        baseUrl: [{ required: true, message: '基础 URL 不能为空', trigger: 'blur' }],
        path: [{ required: true, message: '路径不能为空', trigger: 'blur' }],
        method: [{ required: true, message: '请求方法不能为空', trigger: 'change' }]
      }
    };
  },
  watch: {
    apiConfig: {
      deep: true,
      immediate: true,
      handler(newVal) {
        this.localApiConfig = {
          ...newVal,
          authConfig: newVal.authConfig || {}
        };
      }
    }
  },
  computed: {
    formTitle() {
      return this.localApiConfig.id? '编辑 API 配置' : '新增 API 配置';
    }
  },
  mounted() {
    this.loadSelectAuthValue();
  },
  methods: {
    async loadSelectAuthValue() {
      const response = await getAuthConfigs();
      if (response.data.success) {
        this.selectAuthList = response.data.data.map(item => {
          return {
            value: item.id,
            label: item.name
          };
        });
      } else {
        this.$message.error(response.data.message || '获取数据失败');
      }
    },
    close() {
      this.localApiConfig = {
        authConfig: {}
      };
      this.$refs.form.clearValidate();
      this.$emit('close');
    },
    save() {
      this.$refs.form.validate((valid) => {
        if (valid) {
          this.$emit('save', {...this.localApiConfig});
        }
      });
    }
  }
};
</script>